import java.util.Scanner;


public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        String[][] arr = new String[n][3];
        while (count != n) {
            String s = sc.nextLine();
            arr[count][0] = s;
            arr[count][1] = s.indent(18);
            arr[count][2] = s.indent(18);
            count++;
        }
        int num = sc.nextInt();
        int[] arr1 = new int[num];
        for (int i = 0; i < num; i++) {
            arr1[i] = sc.nextInt();
        }
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if(arr1[i] == (arr[j][1].charAt(0)) - '0') {
                    System.out.println(arr[j][0] + " " +arr[j][2]);
                }
            }
        }
    }
}
